using System;
using System.IO;
using System.Net;
using System.Runtime.Serialization;
using System.Text;

namespace Microsoft.Coding4Fun.TweetCraft.Tests.Support.Mocks
{

	/// <summary>
	/// MockWebResponse
	/// </summary>
	internal class MockWebResponse : HttpWebResponse
	{
		private readonly string contentDownloaded;
		private readonly MemoryStream stream;

		public MockWebResponse(string content)
#pragma warning disable 612,618
			: base(CreatMockWebResponseSerializationInfo(), new StreamingContext())
#pragma warning restore 612,618
		{
			contentDownloaded = content;
			stream = new MemoryStream(Encoding.UTF8.GetBytes(contentDownloaded));
		}


		/// <summary>
		/// Creats the mock web response serialization info.
		/// </summary>
		/// <returns></returns>
		private static SerializationInfo CreatMockWebResponseSerializationInfo()
		{
			var serializationInfo = new SerializationInfo(typeof(MockRequest), new FormatterConverter());
			serializationInfo.AddValue("m_HttpResponseHeaders", new WebHeaderCollection());
			serializationInfo.AddValue("m_StatusDescription", "OK");
			serializationInfo.AddValue("m_ContentLength", 1024);
			serializationInfo.AddValue("m_MediaType", "html");
			serializationInfo.AddValue("m_Verb", "GET");
			serializationInfo.AddValue("m_Certificate", null);
			serializationInfo.AddValue("m_Version", new Version(1, 1));
			serializationInfo.AddValue("m_StatusCode", HttpStatusCode.OK);
			serializationInfo.AddValue("m_Uri", new Uri("http://localhost"));

			return serializationInfo;
		}

		/// <summary>
		/// Gets the stream that is used to read the body of the response from the server.
		/// </summary>
		/// <returns>
		/// A <see cref="T:System.IO.Stream"/> containing the body of the response.
		/// </returns>
		/// <exception cref="T:System.Net.ProtocolViolationException">
		/// There is no response stream.
		/// </exception>
		/// <exception cref="T:System.ObjectDisposedException">
		/// The current instance has been disposed.
		/// </exception>
		/// <PermissionSet>
		/// 	<IPermission class="System.Security.Permissions.EnvironmentPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true"/>
		/// 	<IPermission class="System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true"/>
		/// 	<IPermission class="System.Security.Permissions.SecurityPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Flags="UnmanagedCode, ControlEvidence"/>
		/// </PermissionSet>
		public override Stream GetResponseStream()
		{
			return stream;
		}

		/// <summary>
		/// Gets the length of the content returned by the request.
		/// </summary>
		/// <value></value>
		/// <returns>
		/// The number of bytes returned by the request. Content length does not include header information.
		/// </returns>
		/// <exception cref="T:System.ObjectDisposedException">
		/// The current instance has been disposed.
		/// </exception>
		public override long ContentLength
		{
			get
			{
				return stream.Length;
			}
		}

		/// <summary>
		/// Closes the response stream.
		/// </summary>
		/// <PermissionSet>
		/// 	<IPermission class="System.Security.Permissions.EnvironmentPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true"/>
		/// 	<IPermission class="System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true"/>
		/// 	<IPermission class="System.Security.Permissions.SecurityPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Flags="UnmanagedCode, ControlEvidence"/>
		/// </PermissionSet>
		public override void Close()
		{
			stream.Close();
		}
	}
}